package com.yy.bootdemo.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.bootdemo.admin.entity.BuyUser;
import com.yy.bootdemo.admin.entity.MyGoodsEntity;
import com.yy.bootdemo.admin.service.MyGoodsService;
import com.yy.bootdemo.security.dto.JwtUserDto;
import com.yy.bootdemo.security.utils.PageUtils;
import com.yy.bootdemo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;



/**
 * 商品管理
 *
 * @author yy
 * @email yangxxil01@sina.com
 * @date 2023-01-09 16:32:38
 */
@RestController
@RequestMapping("goods")
public class MyGoodsController {
    @Autowired
    private MyGoodsService myGoodsService;

    /**
     * 列表
     */
    @RequestMapping("/pagelist")
    public R pagelist(@RequestBody PageUtils params){
      

        // 条件构造器
        QueryWrapper<MyGoodsEntity> userQuery = Wrappers.query();


        Page<MyGoodsEntity> page = new Page<>(params.getCurrent(), params.getSize());

        IPage<MyGoodsEntity> userPage = myGoodsService.page(page, userQuery);


        return R.ok(userPage);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{goodsId}") 
    public R info(@PathVariable("goodsId") Integer goodsId){
		MyGoodsEntity myGoods = myGoodsService.getById(goodsId);

        return R.ok( myGoods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save") 
    public R save(@RequestBody MyGoodsEntity myGoods){
		myGoodsService.save(myGoods);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update") 
    public R update(@RequestBody MyGoodsEntity myGoods){
		myGoodsService.updateById(myGoods);

        return R.ok();
    }



    @RequestMapping("/cacheGoods")
    public R cacheGoods(@RequestBody MyGoodsEntity myGoods){
        return  myGoodsService.cacheGoods(myGoods.getGoodsId());
    }
    /**
     * 删除
     */
    @RequestMapping("/delete") 
    public R delete(@RequestBody Integer[] goodsIds){
		myGoodsService.removeByIds(Arrays.asList(goodsIds));

        return R.ok();
    }
    @RequestMapping("/buy")
    public R buy(@RequestBody MyGoodsEntity myGoods){
        JwtUserDto jwtUserDto = (JwtUserDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userId = jwtUserDto.getMyUser().getUserId();
        return  myGoodsService.buy(myGoods.getGoodsId(),userId);

    }

    @RequestMapping("/lpush")
    public R lpush(@RequestBody MyGoodsEntity myGoods) {
        return myGoodsService.lpush(myGoods.getGoodsId());
    }
    @RequestMapping("/lpushbuy")
    public R lpushbuy(@RequestBody BuyUser buyUser) {

        JwtUserDto jwtUserDto = (JwtUserDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userId = jwtUserDto.getMyUser().getUserId();
        buyUser.setUserId((long)userId);
        return myGoodsService.lpushbuy(buyUser);
    }

}
